package com.wumao.controller;

import com.wumao.service.BlogService;
import com.wumao.service.TagService;
import com.wumao.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @author Gaowumao
 * @Decripted
 * @Email none-j@qq.com
 * @create 2021-04-14 14:27
 */
@Controller
public class IndexController {

    @Autowired
    BlogService blogService;
    @Autowired
    TypeService typeService;
    @Autowired
    TagService tagService;

    @RequestMapping("/")
    public String index(@PageableDefault(size = 4,sort ={"updateTime"},direction = Sort.Direction.DESC)Pageable pageable
    , Model model){
        model.addAttribute("page",blogService.listBlog(pageable));
        model.addAttribute("types",typeService.listTypeTop(6));
        model.addAttribute("tags",tagService.listTagTop(10));
        model.addAttribute("recommendBlogs",blogService.listRecommendBlogTop(4));
        return "index_copy";
    }

    @RequestMapping("/search")
    public String search(@PageableDefault(size = 5,sort ={"updateTime"},direction = Sort.Direction.DESC)Pageable pageable
            , @RequestParam String query, Model model){
        model.addAttribute("page",blogService.listBlog("%"+query+"%",pageable));
        model.addAttribute("query",query);
        return "search_copy";
    }

    @RequestMapping("/blog/{id}")
    public String blog(@PathVariable Long id, Model model) {
        model.addAttribute("blog",blogService.getAndConvert(id));
        return "blog_copy";
    }

    @RequestMapping("/about")
    public String about(){
        return "about_copy";
    }


}
